package com.example.task1.ui.AllService.pet;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.PetChattingBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.Arrays;
import java.util.HashMap;

import retrofit2.Call;

public class PetAskDetailsActivity extends AppCompatActivity {

    private RecyclerView rvList;
    private EditText etSave;
    private Button btnSend;
    private ChattingAdapter mChattingAdapter;
    private LinearLayoutManager mLinearLayoutManager;
    private int mId;
    private int pageNum = 1;
    private boolean isTrue = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pet_ask_details);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("问诊详情");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        mId = getIntent().getIntExtra("id", 0);
        initView();
        initData();
        setAdapter();
        loadCommentList();
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = etSave.getText().toString();
                if (!TextUtils.isEmpty(s)) {
                    etSave.setText("");
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("content",s);
                    map.put("inquiryId",mId);
                    PetChattingBean.RowsBean rowsBean = new PetChattingBean.RowsBean();
                    rowsBean.setContent(s);
                    mChattingAdapter.setData(Arrays.asList(rowsBean));
                    Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/pet-hospital/inquiry-message", Api.getBody(map));
                    Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                        @Override
                        public void success(ApiResp data) {
                            MyApplication.show(data.getMsg());
                        }

                        @Override
                        public void error(String msg) {

                        }
                    });
                }else {
                    etSave.setError("评论不能为空！");
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void setAdapter() {
        mChattingAdapter = new ChattingAdapter(this);
        mLinearLayoutManager = new LinearLayoutManager(this);
        rvList.setAdapter(mChattingAdapter);
        rvList.setLayoutManager(mLinearLayoutManager);
    }

    private void loadCommentList() {
        rvList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                if (mChattingAdapter.getItemCount() - 1 == mLinearLayoutManager.findLastVisibleItemPosition() && isTrue) {
                    isTrue = false;
                    initData();
                }
            }
        });
    }

    private void initData() {
        Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/pet-hospital/inquiry-message/list?pageNum="+pageNum+"&pageSize=30&inquiryId="+mId);
        Api.send(apiRespCall, PetChattingBean.class, new CallBack<PetChattingBean>() {
            @Override
            public void success(PetChattingBean data) {
                if (mChattingAdapter.getData().size() < data.getTotal()) {
                    pageNum++;
                    isTrue = true;
                    mChattingAdapter.setData(data.getRows());
                } else {
                    MyApplication.show("没有更多数据了");
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    private void initView() {
        rvList = findViewById(R.id.rv_list);
        etSave = findViewById(R.id.et_save);
        btnSend = findViewById(R.id.btn_send);
    }
}